package cn.sxt.back.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import cn.sxt.common.struts2.action.BaseAction;
import cn.sxt.common.util.ConstatFinalUtil;
import cn.sxt.common.util.EncryptUtil;
import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.users.pojo.AAdmins;
import cn.sxt.users.pojo.ARole;
import cn.sxt.users.service.IUsersService;

/**
 * 需要登陆的Action
 * @author wangshSxt
 */
@Component("adminsBackAction")
@Scope("prototype")
public class AdminsBackAction extends BaseAction
{
	@Resource
	private IUsersService usersService ; 
	
	/* 管理员对象 */
	private AAdmins admins ; 
	
	/**
	 * 登陆后的首页
	 * @return
	 */
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("---- main ----");
		return "main" ; 
	}
	
	/**
	 * 左侧菜单切换
	 * @return
	 */
	public String leftMenu()
	{
		ConstatFinalUtil.LOGGER.info("---- leftMenu ----");
		return "leftMenu" ; 
	}
	
	/**
	 * 查询管理员列表
	 * @return
	 */
	public String adminsList()
	{
		ConstatFinalUtil.LOGGER.info("---- adminsList ----");
		/* 接收参数 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		String roleId = request.getParameter("roleId");
		
		/* 处理参数 */
		if(keyword == null)
		{
			keyword = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(roleId == null)
		{
			roleId = "" ; 
		}
		
		Date stDate = null ;
		Date edDate = null ; 
		
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			/* 从String变成Date类型 */
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}
		
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		
		/* 查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照关键字搜索 */
		condMap.put("keyword", keyword);
		/* 按照状态搜索 */
		condMap.put("status", status);
		
		/* 按照时间搜索 */
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		/* 按照角色Id查询 */
		condMap.put("roleId", roleId);
		
		/* 查询数据 */
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil,condMap);
		/* 存储数据 */
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 查询所有启用的角色 */
		condMap.clear();
		condMap.put("status", "1");
		List<ARole> roleList = this.usersService.findCondListRoleService(null, condMap);
		/* 存储角色列表 */
		request.setAttribute("roleList", roleList);
		
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		request.setAttribute("roleId", roleId);
		return "adminsList" ;
	}
	
	/**
	 * 打开管理员添加的页面
	 * @return
	 */
	public String adminsInsert()
	{
		ConstatFinalUtil.LOGGER.info("---- adminsInsert ----");
		/* 查询所有启用的角色 */
		Map<String, Object> condMap = new HashMap<>() ;
		condMap.put("status", "1");
		List<ARole> roleList = this.usersService.findCondListRoleService(null, condMap);
		/* 存储角色列表 */
		request.setAttribute("roleList", roleList);
		return "adminsInsert" ; 
	}
	
	/**
	 * 管理员添加页面提交
	 * @return
	 * @throws IOException 
	 */
	public String adminsInsertSubmit() throws IOException
	{
		ConstatFinalUtil.LOGGER.info("---- adminsInsertSubmit ----");
		/* 将角色重新查询一遍 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", this.admins.getRole().getId());
		ARole role = this.usersService.findOneRoleService(condMap);
		this.admins.setRole(role);
		this.admins.setPassword(EncryptUtil.encodeStr(this.admins.getPassword()));
		
		this.admins.setCreateTime(new Date());
		this.admins.setUpdateTime(new Date());
		this.admins.setLastLoginTime(new Date());
		JSONObject resultDBJSON = this.usersService.saveOneAdminsService(admins);
		ConstatFinalUtil.LOGGER.info("--结果:{}--",resultDBJSON);
		/*return "adminsInsertSubmit" ;*/
		/* { "statusCode":"200", "message":"\u64cd\u4f5c\u6210\u529f", "navTabId":"", "rel":"", "callbackType":"", "forwardUrl":"", "confirmMsg":"" }  
		JSONObject resultJSON = new JSONObject();
		resultJSON.put("statusCode", "200");
		resultJSON.put("message", resultDBJSON.get("info"));
		resultJSON.put("navTabId", "");
		resultJSON.put("rel", "");
		resultJSON.put("callbackType", "");
		resultJSON.put("forwardUrl", "");
		resultJSON.put("confirmMsg", "");*/
		/* 返回J-ui要求的JSON */
		/*PrintWriter out = response.getWriter() ; 
		out.println(resultJSON);
		out.flush();
		out.close();*/
		
		/* 封装后的结果 */
		JSONObject resultJSON = this.printJUIResult(resultDBJSON.getString("info"));
		this.printOut(resultJSON.toJSONString());
		return null ; 
	}
	
	/**
	 * 打开更新页面
	 * @return
	 */
	public String adminsUpdate()
	{
		ConstatFinalUtil.LOGGER.info("---- adminsUpdate ----");
		/* 接收参数 */
		String aid = request.getParameter("aid");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询管理员 */
		condMap.put("id", aid);
		this.admins = this.usersService.findOneAdminsService(condMap);
		
		/* 查询状态为启用的角色 */
		condMap.clear();
		condMap.put("status", "1");
		List<ARole> roleList = this.usersService.findCondListRoleService(null, condMap);
		/* 存储角色列表 */
		request.setAttribute("roleList", roleList);
		return "adminsUpdate" ; 
	}
	
	/**
	 * 更新页面提交
	 * @return
	 * @throws IOException 
	 */
	public String adminsUpdateSubmit() throws IOException
	{
		ConstatFinalUtil.LOGGER.info("---- adminsUpdateSubmit ----");
		JSONObject resultJSON = new JSONObject() ;
		/* 接收参数 */
		String aid = request.getParameter("aid");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Id查询管理员 */
		condMap.put("id", aid);
		this.admins = this.usersService.findOneAdminsService(condMap);
		
		String email = request.getParameter("email");
		String trueName = request.getParameter("trueName");
		String phone = request.getParameter("phone");
		String qq = request.getParameter("qq");
		String status = request.getParameter("status");
		String roleId = request.getParameter("roleId");
		
		/* 按照id查询角色 */
		condMap.clear();
		condMap.put("id", roleId);
		ARole role = this.usersService.findOneRoleService(condMap);
		this.admins.setRole(role);
		
		this.admins.setEmail(email);
		this.admins.setTrueName(trueName);
		this.admins.setPhone(phone);
		this.admins.setQq(qq);
		this.admins.setStatus(Byte.valueOf(status));
		
		/* 验证邮箱不能重复 */
		condMap.clear();
		condMap.put("email", admins.getEmail());
		AAdmins adminsRes = this.usersService.findOneAdminsService(condMap);
		/* 
		 * ~修改其它的字段,木有修改邮箱,肯定可以查询出来,并且是它自己
		 * ~修改了邮箱,能够查询出来,不应该提交成功
		 * */
		if(adminsRes != null && adminsRes.getId() != admins.getId())
		{
			/* 封装后的结果*/ 
			resultJSON = this.printJUIResult("邮箱已经存在,请更换;id:" + adminsRes.getId());
		}else
		{
			JSONObject resultDBJSON = this.usersService.updateOneAdminsService(admins);
			/* 封装后的结果 */
			resultJSON = this.printJUIResult(resultDBJSON.getString("info"));
		}
		this.printOut(resultJSON.toJSONString());
		return null ; 
	}
	
	/**
	 * 菜单的树形结构
	 * @return
	 */
	public String menuTreeList()
	{
		ConstatFinalUtil.LOGGER.info("---- adminsUpdate ----");
		/* 查询树形结构 */
		String treeStr = this.usersService.menuTreeService(null);
		request.setAttribute("treeStr", treeStr);
		return "menuTreeList" ; 
	}

	public AAdmins getAdmins()
	{
		return admins;
	}

	public void setAdmins(AAdmins admins)
	{
		this.admins = admins;
	}
}
